package com.zxjbyte.yiyi.framework.common.core.okhttp.callback;

import com.zxjbyte.yiyi.framework.common.core.okhttp.IResponseProcessor;

import java.util.function.Consumer;

/**
 * 默认异步回调处理类
 *
 * @Author zhangxingjia
 * @Date 2024/4/28 14:37
 * @Version: 1.0
 */
public class DefaultAsyncCallbackHandler extends AbstractAsyncCallback {

    private Consumer<IResponseProcessor> successConsumer;
    private Consumer<FailureContext> failureConsumer;


    public DefaultAsyncCallbackHandler(Consumer<IResponseProcessor> successConsumer,
                                       Consumer<FailureContext> failureConsumer){
        this.successConsumer = successConsumer;
        this.failureConsumer = failureConsumer;
    }

    public DefaultAsyncCallbackHandler(){
        this.successConsumer = DEFAULT_SUCCESS_CONSUMER;
        this.failureConsumer = DEFAULT_FAIL_CONSUMER;
    }

    public static DefaultAsyncCallbackHandler create(){
        return new DefaultAsyncCallbackHandler();
    }

    public DefaultAsyncCallbackHandler onFailure(Consumer<FailureContext> failureConsumer){
        this.failureConsumer = failureConsumer;
        return this;
    }

    public DefaultAsyncCallbackHandler onSuccess(Consumer<IResponseProcessor> successConsumer) {
        this.successConsumer = successConsumer;
        return this;
    }

    @Override
    protected final void asFailure(FailureContext context) {
        failureConsumer.accept(context);
    }

    @Override
    protected final void asSuccess(IResponseProcessor processor) {
        successConsumer.accept(processor);
    }
}
